﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using V5_Common;
using V5_BLL;
using V5_Model;
using V5_EShop._Class.BLL.GZ;

namespace V5_EShop.Admin {
    public partial class Admin_ProductOrder : BasicPage {
        const string vProductOrder = "vProductOrder";
        public BLLGZProductOrder bllGZProductOrder = new BLLGZProductOrder();
        public BLLGZUser bllGZUser = new BLLGZUser();
        public ModelGZProductOrder m_GZProductOrder = new ModelGZProductOrder();
        public ModelGZUser m_GZUser = new ModelGZUser();
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                Action = InputHelper.SetString(Request["Action"]);
                string OrderID = InputHelper.SetString(Request["OrderID"]);
                switch (Action) {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        if (OrderID.Trim() != "") {
                            Bind_View(new Guid(OrderID));
                        }
                        break;
                    default:
                        this.Bind_DataList();
                        break;
                }
            }
        }

        private void Bind_DataList() {
            this.MultiViewList.ActiveViewIndex = 0;
            string strWhere = "";
            if (ViewState[vProductOrder] != null && InputHelper.SetString(ViewState[vProductOrder]) != "") {
                strWhere = InputHelper.SetString(ViewState[vProductOrder]);
            }
            int count = 0;
            DataSet ds = bllGZProductOrder.GetList("*", Pager.PageSize, Pager.CurrentPageIndex, strWhere, " CreateDatetime", ref count);
            Pager.RecordCount = count;
            this.gvList.DataSource = ds;
            this.gvList.DataBind();
        }

        protected void Pager_PageChanged(object sender, EventArgs e) {
            this.Bind_DataList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e) {
            string eName = InputHelper.SetString(e.CommandName);
            Guid orderID = new Guid(InputHelper.SetString(e.CommandArgument));
            switch (eName.ToLower()) {
                case "view":
                    Bind_View(orderID);
                    break;
                case "del":
                    this.Bind_Delete(orderID);
                    break;
            }
        }

        private void Bind_View(Guid id) {
            this.MultiViewList.ActiveViewIndex = 1;
            m_GZProductOrder = bllGZProductOrder.GetModel(id);
            m_GZUser = bllGZUser.GetModel(m_GZProductOrder.CustomID);

            this.hfID.Value = id.ToString();
        }

        private void Bind_Delete(Guid id) {
            DelParentOrder(id.ToString());
            this.Bind_DataList();
        }

        public void Bind_OrderProduct(string OrderNum) {
            this.rpProductList.DataSource = new BLLGZProductOrderItem().GetList(0, " ParentID='" + OrderNum + "' ", " ID Desc");
            this.rpProductList.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var id = hfID.Value;

            if (cb5.Checked)
            {
                DelParentOrder(id);
            }
            Response.Redirect("/Admin/GZProductOrder/Admin_ProductOrder.aspx");
            Response.End();

        }

        public string DelParentOrder(string guid)
        {
            DataTable dt2 = new V5_EShop._Class.BLL.GZ.BLLGZProductOrderItem().GetList(" ParentID='" + guid + "'").Tables[0];

            if (dt2 != null && dt2.Rows.Count > 0)
            {
                foreach (DataRow dr in dt2.Rows)
                {
                    new V5_EShop._Class.BLL.GZ.BLLGZProductOrderItem().Delete(Guid.Parse(dr["ID"] + string.Empty));
                }
            }
            new V5_EShop._Class.BLL.GZ.BLLGZProductOrder().Delete(Guid.Parse(guid));

            return "0";
        }
    }
}
